package org.luxor.commons.sms.service.impl;

import org.luxor.commons.sms.client.AbstractSmsClient;
import org.luxor.commons.sms.client.aliyun.AliyunSmsClient;
import org.luxor.commons.sms.client.qcloud.QcloudSmsClient;
import org.luxor.commons.sms.config.properies.SmsProperties;
import org.luxor.commons.sms.constant.SmsType;
import org.luxor.commons.sms.entity.PhoneNumber;
import org.luxor.commons.sms.exception.SmsTypeNotSupportedExection;
import org.luxor.commons.sms.service.ISmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * OSS文件存储服务
 *
 * @author Mr.Yan  @date 2019/12/20
 */
@Service
public class SmsServiceImpl implements ISmsService {

    private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

    @Resource
    private SmsProperties smsProperties;
    @Resource
    private AliyunSmsClient aliyunSmsClient;
    @Resource
    private QcloudSmsClient qcloudSmsClient;

    /**
     * 存储方式选择器
     *
     * @param smsType (1：极光推送 2：阿里云 3：腾讯云)
     * @return SmsClient
     */
    private AbstractSmsClient build(SmsType smsType) throws SmsTypeNotSupportedExection {
        switch (smsType) {
            case qcloud:
                return qcloudSmsClient;
            case aliyun:
                return aliyunSmsClient;
            default:
                logger.error("短信服务类型(smsType):[{}],目前暂不支持", smsType);
                throw new SmsTypeNotSupportedExection("短信服务类型(smsType):[" + smsType + "],目前暂不支持");
        }
    }

    @Override
    public void sendMessage(Set<PhoneNumber> phoneNumbers, String templateId, Map<String, String> context) {
        SmsType smsType = smsProperties.getSmsType();
        this.build(smsType).sendMessage(phoneNumbers, templateId, context, "发送人");
    }

    @Override
    public void sendMessage(PhoneNumber phoneNumber, String templateId, Map<String, String> context) {
        Set<PhoneNumber> phoneNumbers = new HashSet<>();
        phoneNumbers.add(phoneNumber);
        this.sendMessage(phoneNumbers, templateId, context);
    }
}
